import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Login',
      component: () => import('../views/Login'),
    },
    {
      path: '/home',
      name: 'home',
      redirect: '/student',
      component: () => import('../views/Home'),
      children: [
        {path: '/student',  name: 'student', component: () => import("../pages/student")},
        {path: '/teacher',  name: 'teacher', component: () => import("../pages/teacher")},
        {path: '/class',  name: 'class', component: () => import("../pages/class")},
        {path: '/speciality',  name: 'speciality', component: () => import("../pages/speciality")},
        {path: '/questions',  name: 'questions', component: () => import("../pages/questions")},
        {path: '/questionGroup',  name: 'questionGroup', component: () => import("../pages/questionGroup")},
        {path: '/achievement',  name: 'achievement', component: () => import("../pages/achievement")},
        {path: '/classResults',  name: 'classResults', component: () => import("../pages/classResults")},
      ]
    }
  ]

})
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (!token) {
      next('/')
    } else {
      next()
    }
  }
})

export default router
